﻿    using System;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using System.Web.Security;
    using Microsoft.SharePoint.Administration;

namespace SoftwareDiscipline.SharePoint.UserManagement.ApplicationPages
{

    /// <summary>
    /// 
    /// </summary>
    public partial class EditUserPage : LayoutsPageBase
    {
        /// <summary>
        /// 
        /// </summary>
        private Guid? providerKey = null;

        /// <summary>
        /// 
        /// </summary>
        private MembershipUser user = null;

        /// <summary>
        /// Represents that method that handles the <see cref="E:System.Web.UI.Control.Load"/> event of the page.
        /// </summary>
        /// <param name="e">A <see cref="T:System.EventArgs"/> that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!this.Page.IsPostBack)
            {
                if (this.User != null)
                {
                    this.labelUserName.Text = this.User.UserName;
                    this.textEmailAddress.Text = this.User.Email;
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.User != null)
                {
                    SqlMembershipProvider provider = this.GetMembershipProvider();
                    if (provider != null)
                    {
                        if (!this.User.Email.Equals(this.textEmailAddress.Text, StringComparison.OrdinalIgnoreCase))
                        {
                            this.User.Email = this.textEmailAddress.Text;
                            provider.UpdateUser(this.User);
                        }
                    }

                    this.CloseWindow();
                }
            }
            catch (Exception ex)
            {
                this.LabelErrorMessage.Text = ex.Message;
            }
        }

        /// <summary>
        /// Closes the window.
        /// </summary>
        private void CloseWindow()
        {
            if (!this.Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "__SW_UserManagement_NewUser_CloseWindow"))
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "__SW_UserManagement_NewUser_CloseWindow", "window.frameElement.commitPopup();", true);
            }
        }

        /// <summary>
        /// Gets the provider key passed from the 
        /// </summary>
        /// <value>The provider key.</value>
        protected Guid ProviderKey
        {
            get
            {
                if (this.providerKey == null)
                {
                    string keyParam = this.Page.Request.QueryString["Id"];
                    if (!String.IsNullOrEmpty(keyParam))
                    {
                        this.providerKey = new Guid(keyParam);
                    }
                }

                return this.providerKey.Value;
            }
        }

        /// <summary>
        /// Gets information about the user making the page request.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// An <see cref="T:System.Security.Principal.IPrincipal"/> that represents the user making the page request.
        /// </returns>
        protected MembershipUser User
        {
            get
            {
                if (this.user == null)
                {
                    if (this.ProviderKey != Guid.Empty)
                    {
                        SqlMembershipProvider provider = this.GetMembershipProvider();
                        if (provider != null)
                        {
                            this.user = provider.GetUser(providerKey, false);
                        }
                    }
                }

                return this.user;
            }
        }

        /// <summary>
        /// Gets the membership provider.
        /// </summary>
        /// <returns></returns>
        private SqlMembershipProvider GetMembershipProvider()
        {
            SPWebApplication webApp = SPContext.Current.Site.WebApplication;
            if (webApp != null)
            {
                // find the current zone
                SPUrlZone currentZone = SPContext.Current.Site.Zone;

                return webApp.GetSqlMembershipProvider(currentZone);
            }

            return null;
        }
    }
}
